package com.geen.tuoke.ui.viewmodel

import android.view.Menu
import android.view.View
import androidx.lifecycle.MutableLiveData
import com.geen.tuoke.R
import com.geen.tuoke.base.BaseViewModel
import com.geen.tuoke.bean.MenuCode
import com.geen.tuoke.bean.MenuItem
import com.geen.tuoke.bean.PayMoneyInfo
import com.geen.tuoke.bean.UserInfo
import com.geen.tuoke.network.api.apiService
import com.geen.tuoke.network.utils.CacheUtil
import com.geen.tuoke.utils.Arith
import com.geen.tuoke.utils.DateUtils
import com.geen.tuoke.utils.ext.request
import com.geen.tuoke.utils.ext.toastShort
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File

/**
 * @Author LuoJi
 * @Date 2021/12/29-13:51
 * @Desc
 */
class MeViewModel:BaseViewModel() {

    var menuItemList = mutableListOf<MenuItem>()
    var userInfo = MutableLiveData<UserInfo>()
    var showBalancePlaceHolder = MutableLiveData<Boolean>()
    var showMemberLimitTimePlaceHolder = MutableLiveData<Boolean>()
    var uploadAvatarSuccess = MutableLiveData<String>()
    var payMoneyList = MutableLiveData<MutableList<PayMoneyInfo>>()

    init {
        menuItemList.add(MenuItem(MenuCode.话费充值.ordinal,MenuCode.话费充值.name, R.mipmap.icon_charge))
        menuItemList.add(MenuItem(MenuCode.版本更新.ordinal,MenuCode.版本更新.name,R.mipmap.icon_version))
        menuItemList.add(MenuItem(MenuCode.客服服务.ordinal,MenuCode.客服服务.name,R.mipmap.icon_customer_service))
        menuItemList.add(MenuItem(MenuCode.用户协议.ordinal,MenuCode.用户协议.name,R.mipmap.icon_agreement))
        menuItemList.add(MenuItem(MenuCode.隐私政策.ordinal,MenuCode.隐私政策.name,R.mipmap.icon_privacy))
    }

    fun reloadUserInfo(){
        getPhoneBalance()
        getMemberLimit()
    }

    fun getPhoneBalance():String{
        userInfo.value?.let {
            showBalancePlaceHolder.value = false
            if(it.balance==0f){
                showBalancePlaceHolder.value = true
            }
            return Arith.getDecimalString(it.balance)
        }
        return ""
    }

    fun getMemberLimit():String{
        userInfo.value?.let {
            showMemberLimitTimePlaceHolder.value =false
            if(it.memberEnd==0L){
                showMemberLimitTimePlaceHolder.value = true
            }
            return DateUtils.formatCreateTime(it.memberEnd)
        }
        return ""
    }

    fun uploadAvatar(file: File) {
        val body: RequestBody =
            RequestBody.create(MediaType.parse("multipart/form-data"), file) //说明该文件为图片类型
        val part = MultipartBody.Part.createFormData("file", file.name, body)
        request({ apiService.upLoadAvatar(part) }, {
           uploadAvatarSuccess.value = file.absolutePath
        }, {
            uploadAvatarSuccess.value = ""
            toastShort(it.errorMsg)
        }, true)
    }

    fun getPayMoneyList(){
        request({ apiService.getPayMoneyList()},{
            payMoneyList.value = it
        },{

        },true)
    }

    interface MeHandler{

        fun clickPhoto(view: View)

        fun loginOut(view:View)
    }
}